package cc.magicjson.easy.batch.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BatchResult<T> {
    private boolean successful;
    private int totalItems;
    private int affectedRows;
    private String errorMessage;
    private long executionTime;
    private List<T> failedItems = new ArrayList<>();

    public void merge(BatchResult<T> other) {
        this.totalItems += other.totalItems;
        this.affectedRows += other.affectedRows;
        this.successful &= other.successful;
        if (!other.successful) {
            this.errorMessage = (this.errorMessage == null ? "" : this.errorMessage + "; ")
                + other.errorMessage;
            this.failedItems.addAll(other.failedItems);
        }
    }
}
